/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package info.toegepaste.www.dataaccess;

import info.toegepaste.www.beans.Werkstatus;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Logic
 */
public class DAWerkstatus {
    private Connection connection = null;

    public DAWerkstatus(String url, String login, String password) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection(url, login, password);
    }

    public DAWerkstatus(Connection conn) {
        connection = conn;
    }
    
    public void close() throws SQLException {
        if (connection != null) {
            connection.close();
        }
    }
    
    public Werkstatus getWerkstatus(int id) {
        Werkstatus werkstatus = new Werkstatus();
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            statement = connection.prepareStatement("SELECT  * FROM werkstatus where werkstatusID = ?");
            statement.setInt(1, id);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                werkstatus.setWerkstatusID(resultSet.getInt("werkstatusID"));
                werkstatus.setWerkstatusNaam(resultSet.getString("werkstatusnaam"));
            } 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                statement.close();
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (SQLException e) {
            }
        }
        return werkstatus;
    }
    public ArrayList<Werkstatus> getAlleWerkstatussen() {
        ArrayList<Werkstatus> resultaat = new ArrayList<Werkstatus>();
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            statement = connection.prepareStatement("select * from werkstatus");
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Werkstatus werkstatus = new Werkstatus();
                werkstatus.setWerkstatusID(resultSet.getInt("werkstatusID"));
                werkstatus.setWerkstatusNaam(resultSet.getString("werkstatusnaam"));
                
                resultaat.add(werkstatus);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                statement.close();
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (SQLException e) {
            }
        }
        return resultaat;
    }
    
    public boolean insertWerkstatus(int werkstatusID, String werkstatusnaam) {
        boolean resultaat = true;
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement("insert into werkstatus values(?, ?)");
            statement.setInt(1, werkstatusID);
            statement.setString(2, werkstatusnaam);
            statement.executeUpdate();
            
        } catch (Exception e) {
            resultaat = false;
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException e) {
            }
        }
        return resultaat;
    }
}
